// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using NUnit.Framework;
using System;
using System.Text;
using SoapTrace.Core.Communication;

namespace SoapTrace.Tests
{
	/// <summary>
	/// Test fixture to test soap response parsing. 
	/// </summary>
	[ TestFixture ]
	public class SoapResponseTestFixture
	{
		/// <summary>
		/// The complete http message.
		/// </summary>
		private string m_HttpResponseMessage = String.Empty;

		/// <summary>
		/// The Http header.
		/// </summary>
		private string m_HttpHeader = "HTTP/1.1 200 OK\r\nServer: Microsoft-IIS/5.0\r\nDate: Sun, 04 Jul 2004 17:20:04 GMT\r\nX-Powered-By: ASP.NET\r\nX-AspNet-Version: 1.1.4322\r\nCache-Control: private, max-age=0\r\nContent-Type: text/xml; charset=utf-8\r\nContent-Length: 327\r\n\r\n";
		
		/// <summary>
		/// The Http message body.
		/// </summary>
		private string m_HttpMessageBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><AddResponse xmlns=\"http://tempuri.org/\"><AddResult>121</AddResult></AddResponse></soap:Body></soap:Envelope>";
		
		/// <summary>
		/// The soap response object under test.
		/// </summary>
		private SoapResponse m_Response = new SoapResponse( );

		/// <summary>
		/// Initialises each test.
		/// </summary>
		[ SetUp ]
		public void Init( )
		{
			// Create the Http response message.

			m_HttpResponseMessage = String.Concat( m_HttpHeader, m_HttpMessageBody );

			// Parse the message.

			byte[ ] Bytes = UnicodeEncoding.UTF8.GetBytes( m_HttpResponseMessage );

			for( int i = 0; i < Bytes.Length; ++i )
			{
				byte MessageByte = Bytes[ i ];

				bool EndOfMessage = m_Response.AddByte( MessageByte );
				
				if( Bytes.Length == ( i + 1 ) )
				{
					Assert.IsTrue( EndOfMessage, "Should be at the end of the message." );
				}
				else
				{
					Assert.IsFalse( EndOfMessage, "Should not be at the end of the message." );
				}
			}
		}

		/// <summary>
		/// Test that a simple soap message has been parsed as we expect.
		/// </summary>
		[ Test ]
		public void MessageReceived( )
		{
			Assert.AreEqual( m_HttpHeader, m_Response.Header, "Soap Header is wrong." );
			Assert.AreEqual( m_HttpMessageBody, m_Response.Message, "Soap Message is wrong." );
			Assert.AreEqual( UnicodeEncoding.UTF8.GetBytes( m_HttpMessageBody ).Length, m_Response.MessageLength, "Soap message length is wrong." );
		}
		
		/// <summary>
		/// Tests that the SoapResponse object can handle having its
		/// AddByte called after it has received a complete message.
		/// </summary>
		[ Test ]
		public void TwoMessages( )
		{
			Init( );
			
			MessageReceived( );
		}
	}
}
